<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 */

class Cli_outcc {
	public function run($args) {
		
		set_time_limit ( 0 );
		error_reporting(0);
		while (true)
		{
			$data_Sql = "SELECT * FROM fscall.outcall_cc WHERE result = 0  order by id asc";
			$data = QDB::getConn('fscall')->execute($data_Sql)->fetchAll();
			foreach ($data as $k=>$v)
			{
				if($v['type'] == 0)
				{
					$this->outcall($v);
				}else if($v['type'] == 1){
					$this->outNewUser($v);
				}
			}
			usleep(50000);
		}
		
		
		
	}
	/**
	 * 外呼
	 */
	private function outcall($data)
	{
		//更改状态
		QDB::getConn('fscall')->execute("UPDATE fscall.outcall_cc SET calltime = now(),result = 1 WHERE id = {$data['id']}");
		$clisting = "bgapi originate {ignore_early_media=true,proxy_media=true,jitterbuffer_msec=180,call_timeout=50,orig_acct=" . $data['acct'] . ",orig_caller=" . $data['caller'] . ",orig_called=" . $data['oparg'] . "}";
		$callsta = "call fscall.voip('{$data['acct']}','{$data['caller']}',{$data['caller']},7)";
		try {
			$handle = QDB::getConn('fscall')->execute($callsta);
			$calldata = $handle->fetchAll();
			$handle->free();
			QDB::getConn('fscall')->close();
			QDB::getConn('fscall')->connect();
			foreach ($calldata as $k=>$item)
			{
				$item = array_values($item);
				$clisting .= "[calltype=7,execute_on_answer='sched_hangup +" . $item[3] . " alloted_timeout',origination_caller_id_name='" . $data['caller']. "',origination_caller_id_number=" . $data['caller'] . ",absolute_codec_string='" . $item[0] . "']". "sofia/external/" . $item[5] . "@" . $item[2];
				if($k)$clisting.="|";
			}
			$clisting .= " {$data['oparg']}  XML outcc {$data['caller']} {$data['caller']}";
			QDB::getConn('fscall')->execute("UPDATE fscall.outcall_cc SET result = 2 WHERE id = {$data['id']}");
			Helper_Freeswitch::execute($clisting);			
		} catch (Exception $e) {
		}

	}
	/**
	 * 抱密码
	 * @param array $data
	 */
	private function outNewUser($data)
	{
		//更改状态
		QDB::getConn('fscall')->execute("UPDATE fscall.outcall_cc SET calltime = now(),result = 1 WHERE id = {$data['id']}");
		$clisting = "bgapi originate {ignore_early_media=true,proxy_media=true,jitterbuffer_msec=180,call_timeout=50,orig_acct=" . $data['acct'] . ",orig_caller=" . $data['caller'] . ",orig_called=" . $data['oparg'] . "}";
		$callsta = "call fscall.voip('{$data['acct']}','{$data['caller']}',{$data['caller']},7)";
		try {
			
			$handle = QDB::getConn('fscall')->execute($callsta);
			if(is_object($handle))
			{
				$calldata = $handle->fetchAll();
				$handle->free();
				QDB::getConn('fscall')->close();
				QDB::getConn('fscall')->connect();
				
				foreach ($calldata as $k=>$item)
				{
					$item = array_values($item);
					
					$clisting .= "[calltype=1,execute_on_answer='sched_hangup +" . $item[3] . " alloted_timeout',origination_caller_id_name='" . $data['caller']. "',origination_caller_id_number=" . $data['caller'] . ",absolute_codec_string='" . $item[0] . "']". "sofia/external/" . $item[5] . "@" . $item[2];
					if($k)$clisting.="|";
				}
				$clisting .= " {$data['oparg']}  XML balance {$data['caller']} {$data['caller']}";
				echo $clisting;
			}
			QDB::getConn('fscall')->execute("UPDATE fscall.outcall_cc SET result = 2 WHERE id = {$data['id']}");
			Helper_Freeswitch::execute($clisting);
		} catch (Exception $e) {
		}
		
	}
	
}